package com.ten.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.AclRole;
import com.ten.common.core.entity.AclUser;
import com.ten.common.core.entity.AclUserRole;
import com.ten.common.core.vo.Result;
import com.ten.system.dao.AclRoleDao;
import com.ten.system.dao.AclUserRoleDao;
import com.ten.system.service.AclRoleService;
import com.ten.system.vo.FenRoleVo;
import com.ten.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：Q大侠
 * @date ：Created in 2022/12/5 18:33
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class AclRoleServiceImpl implements AclRoleService {
    @Autowired
    private AclRoleDao aclRoleDao;
    @Autowired
    private AclUserRoleDao aclUserRoleDao;
    @Override
    public Result finRoleByUserId(Long userid) {
        //1. 查询所有的角色
        List<AclRole> aclRoles = aclRoleDao.selectList(null);
        //2. 查询当前用户具有的角色
        List<AclRole> userRoles = aclRoleDao.selectByUserid(userid);
        List<String> userRoleIds = userRoles.stream().map(item -> item.getId()).collect(Collectors.toList());
        Map<String,Object> map = new HashMap<>();
        map.put("aclRoles",aclRoles);
        map.put("userRoles",userRoleIds);
        return new Result(2000,"查询成功",map);
    }

    @Override
    @Transactional
    public Result fenUserRole(FenRoleVo fenRoleVo) {
        //删除当前用户具有的角色
        UpdateWrapper<AclUserRole> wrapper=new UpdateWrapper<>();
        wrapper.eq("user_id",fenRoleVo.getUserId());
        aclUserRoleDao.delete(wrapper);
        //添加该用户具有的新角色 insert into 表名 values(值,值...)(值,值...)(值,值....)  动态sql语句
        List<AclUserRole> list=new ArrayList<>();
        for(String roleId:fenRoleVo.getRoleIds()){
            AclUserRole aclUserRole = new AclUserRole();
            aclUserRole.setIsDeleted(0);
            aclUserRole.setRoleId(roleId);
            aclUserRole.setUserId(fenRoleVo.getUserId());
            aclUserRole.setGmtCreate(LocalDateTime.now());
            aclUserRole.setGmtModified(LocalDateTime.now());
            list.add(aclUserRole);
        }
        aclUserRoleDao.batchInsert(list);

        return new Result(2000,"分配角色成功");
    }

    @Override
    public Result findByConditionPage(RoleVo roleVo, Integer current, Integer pageSize) {
        //创建分页工具对象
        IPage<AclRole> page = new Page<>(current,pageSize);
        //条件查询对象
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if (roleVo.getIsDeleted()!=null){
            wrapper.eq("is_delete",roleVo.getIsDeleted());
        }
        IPage<AclRole> page1 = aclRoleDao.selectPage(page,wrapper);
        return new Result(2000,"分页查询角色成功",page1);
    }

    @Override
    public Result updateRoleStatusByid(String id, int deleted) {
        int i = aclRoleDao.updateRoleStatusByid(id, deleted);
        String massage = null;
        if (deleted==1){
            massage = "禁用";
        }else {
            massage = "启用";
        }
        if (i==1){
            return new Result<>(2000,massage+"成功!");
        }return new Result<>(4000,massage+"失败!");
    }

    @Override
    public Result addOrUpdateRole(AclRole aclRole) {
        //判断角色名是否已存在
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name",aclRole.getRoleName());
        if (aclRoleDao.selectOne(wrapper)!=null){
            return new Result<>(4000,"角色名已存在,添加失败!");
        }
        //生成id--首位为1+随机生成十八位随机数
        String sb = "1";
        for (int i = 0; i < 18; i++) {
            sb=sb+(int)(Math.random() * 10);
        }
        //完善需要添加的对象
        aclRole.setId(sb);
        aclRole.setRoleCode("1");
        aclRole.setIsDeleted(0);
        aclRole.setGmtCreate(LocalDateTime.now());
        aclRole.setGmtModified(LocalDateTime.now());
        int insert = aclRoleDao.insert(aclRole);
        if (insert ==1){
            return new Result<>(2000,"添加成功!");
        }
        return new Result<>(4000,"添加失败!");
    }

    @Override
    public Result deleteRoleById(String id) {
        int i = aclRoleDao.deleteById(id);
        if (i ==1){
            return new Result<>(2000,"删除成功!");
        }
        return new Result<>(4000,"删除失败!");
    }

    @Override
    public Result updateRoleById(AclRole aclRole) {
        aclRole.setGmtModified(LocalDateTime.now());
        int i = aclRoleDao.updateById(aclRole);
        if (i ==1){
            return new Result<>(2000,"修改成功!");
        }
        return new Result<>(4000,"修改失败!");
    }
}
